package com.braska.hewankurban;

import com.braska.hewankurban.db.DatabaseHandler;
import com.braska.hewankurban.helper.ServiceHelper;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class BiodataActivity extends Activity {
	EditText nama;
	EditText alamat;
	EditText hp;
	Button submit;
	Intent i;
	ServiceHelper srv = new ServiceHelper();
	
	DatabaseHandler db = new DatabaseHandler(this);
	String id_user;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_biodata);
		nama = (EditText) findViewById(R.id.ed_nama);
		alamat= (EditText) findViewById(R.id.ed_alamat);
		hp = (EditText) findViewById(R.id.ed_nohp);
		submit = (Button) findViewById(R.id.btn_submit);
		i = new Intent(getApplicationContext(), PilihHewanActivity.class);
		
		Bundle extras = getIntent().getExtras();
		 id_user = extras.getString("id");
		
		
		
		submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				i.putExtra("nama", nama.getText().toString());
				 if (nama.getText().toString().trim().equals("")) {
						Toast.makeText(getBaseContext(), "Nama harus di isi", Toast.LENGTH_LONG)
						.show();
						return;
					}
				 
				 if (alamat.getText().toString().trim().equals("")) {
						Toast.makeText(getBaseContext(), "Alamat harus di isi", Toast.LENGTH_LONG)
						.show();
						return;
					}
				 
				 if (hp.getText().toString().trim().equals("")) {
						Toast.makeText(getBaseContext(), "Nomer Telp harus di isi", Toast.LENGTH_LONG)
						.show();
						return;
					}
				 
				 
				new tambahpemiliktask().execute();
			}
		});
		
	}
	
	public class tambahpemiliktask extends AsyncTask<String, Void, Boolean>{

		ProgressDialog dialog = new ProgressDialog(BiodataActivity.this);

		protected void onPreExecute() {
			dialog.setMessage(" Loading... ");
			dialog.show();
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub

			// setList();
			dialog.dismiss();
			
		}

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				String srvres = srv.tambahpemilik(nama.getText().toString(), alamat.getText().toString(),hp.getText().toString());
				if (srvres.equals("1")) {
					Log.d("sukses", srvres);
				
					i.putExtra("id", id_user);
					startActivity(i);
					finish();
				}else {
					Log.d("gagal", srvres);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			return true;
		}
		
	}

}
